package cn.edu.zime.bigdata.controller;

import cn.edu.zime.bigdata.anotations.AnatationTest;
import cn.edu.zime.bigdata.dao.mapper.StudentGradeMapper;
import cn.edu.zime.bigdata.dao.model.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Random;

@RestController
public class StudentGradeController {
    @Autowired
    StudentGradeMapper studentGradeMapper;

    @GetMapping("/addgrade")
    @AnatationTest
    public int addGrade(@RequestParam(required = false) String studentId, @RequestParam(required = false) String year, @RequestParam(required = false) String subject,
        @RequestParam(required = false) String grade, @RequestParam(required = false) String dengji) {
        String id = String.valueOf(Math.abs(new Random().nextInt()));
        return studentGradeMapper.insertGrade(
            new StudentGrade().setId(id).setSubject(subject).setYear(year)
                .setGrade(grade).setDengji(dengji).setStudentId(studentId));
        //        return studentMapperXML.insertStudent(new Student(id, name, genda, level, depart, ability,grade,dengji));
    }


    @AnatationTest
    @GetMapping("/getupStudents")
    public List<StudentGradeStat> selectupStudents(@RequestParam(required = false) String year, @RequestParam(required = false) String subject) {
        System.out.println("-----select");
        return studentGradeMapper.countStudentGrade(new StudentGradeQuery().setYear(year).setSubject(subject));
    }

    @AnatationTest
    @GetMapping("/getAllGrades")
    public List<Student> getAllGrades() {
        System.out.println("-----select--------");
        return studentGradeMapper.selectStudentGrade(new StudentGradeQuery());
    }

    @GetMapping("/getStudentGrades")
    public List<Student> getStudentGrades(@RequestParam(required = false) String studentId, @RequestParam(required = false) String year,
        @RequestParam(required = false) String subject) {
        return studentGradeMapper.selectStudentGrade(
            new StudentGradeQuery().setStudentId(studentId).setYear(year).setSubject(subject));
    }

    @PostMapping("/addgrades")
    public String addgrades(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        //String suffixName = fileName.substring(fileName.lastIndexOf("."));
        Workbook workbook = null;   //定义一个表文件
        workbook = new XSSFWorkbook(file.getInputStream());  //打开.xls类型的文件
        Sheet sheet = workbook.getSheetAt(0);  //打开文件中的第1个sheet
        int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();  //获取总行数
        for (int i = 1; i < physicalNumberOfRows; i++) {   //第一行是属性名，数据记录是从第2行开始
            System.out.println("hangshu:" + physicalNumberOfRows);
            Row row = sheet.getRow(i);  //获取第（i+1）行数据
            if (row == null) {
                continue;
            }
            System.out.println(row);
            row.getCell(1).setCellType(CellType.STRING);
            row.getCell(2).setCellType(CellType.STRING);
            row.getCell(3).setCellType(CellType.STRING);
            row.getCell(4).setCellType(CellType.STRING);
            row.getCell(5).setCellType(CellType.STRING);
            String studentId = row.getCell(1).getStringCellValue();
            String year = row.getCell(2).getStringCellValue();
            String subject = row.getCell(3).getStringCellValue();
            String grade = row.getCell(4).getStringCellValue();
            System.out.println(grade);
            String dengji = row.getCell(5).getStringCellValue();
            System.out.println(dengji);
            String id = String.valueOf(Math.abs(new Random().nextInt()));
            studentGradeMapper.insertGrade(
                new StudentGrade().setId(id).setSubject(subject).setYear(year)
                    .setGrade(grade).setDengji(dengji).setStudentId(studentId));
        }
        return "sucess";
    }

}
